local game = require "club.threecard.blackjack"

local game_ui = game.ui.activity()

function game_ui:ctor()
    self.res = "club.threecard.blackjack/ui/ingame.b:ingame"

    local phase_shower = game.require "ui.ingame.phase_shower"
    self.phase_shower = phase_shower.new();
    local banker = game.require "ui.ingame.banker"
    self.banker = banker.new();
    local bet_chip = game.require "ui.ingame.bet_chip"
    self.bet_chip = bet_chip.new();
    local seats = game.require "ui.ingame.seats"
    self.seats = seats.new(self.bet_chip);
    local dealer = game.require "ui.ingame.dealer"
    self.dealer = dealer.new(self.seats, self.banker);
    local bet_operator = game.require "ui.ingame.bet_operator"
    self.bet_operator = bet_operator.new();
    local actions = game.require "ui.ingame.actions"
    self.actions = actions.new();
    local rule = game.require "ui.ingame.rule"
    self.rule = rule.new();
    local resumer = game.require 'ui.ingame.resumer'
    self.resumer = resumer.new(self);
    local croupier = game.require "ui.ingame.croupier"
    croupier.instance = croupier.new();
    
    self.server_time = require("ui.widgets.ServerTime").new()
    self.network_signal = require("ui.widgets.NetworkSignal").new()

    self.phase_shower:hide();
    self.resumer:hide();
    croupier.instance:hide();
end

function game_ui:on_load()
    self:super("on_load");
    self.phase_shower:load();
    self.phase_shower:set_parent(self.phase_root, false);
    self.banker:load();
    self.banker:set_parent(self.banker_root, false);
    self.seats:load();
    self.seats:set_parent(self.seats_root, false);
    self.dealer:load();
    self.dealer:set_parent(self.dealer_root, false);
    self.bet_operator:load();
    self.bet_operator:set_parent(self.bet_operator_root, false);
    self.actions:load();
    self.actions:set_parent(self.action_root, false);
    self.bet_chip:load();
    self.bet_chip:set_parent(self.bet_chip_root, false);
    local croupier = game.require "ui.ingame.croupier"
    croupier.instance:load();
    croupier.instance:set_parent(self.croupier_root, false);

    self.resumer:load();
    self.resumer:set_parent(self.resumer_root, false);

    game.game_scene.loaded = true
    game.store.ingame.throttle(false)

    self.rule:load();
    self.rule:set_parent(self.rule_root, false);
end

function game_ui:on_injected(bind)
    bind("button.click", self.exit_btn, self.on_exit_game);
    bind("shortcut.escape", self.exit_btn.gameObject, self.on_exit_game);
    bind("button.click", self.help_btn, self.help_ivk);
    bind("gameobject.element", self.network_signal_obj, self.network_signal)
    bind("gameobject.element", self.server_time_obj, self.server_time)
end

function game_ui:clear_scene()
    game.coroutine.ingame_stop_all()
    self.banker:clear_scene()
    self.seats:clear_scene()
    local croupier = game.require "ui.ingame.croupier"
    croupier.instance:clear_scene()
    self.bet_chip:clear()
    self.phase_shower:hide()
end

function game_ui:help_ivk()
    game.audio.post("blackjack:click");
    game.router.push("/club.threecard.blackjack/manual")
end

function game_ui:on_exit_game()
    self:clear_scene()
    game.audio.post("blackjack:click");
    game.game_scene.leave();
end

function game_ui:on_release()
    self.phase_shower:release()
    self.seats:release()
end

return game_ui